<?

class graphBase {
	protected $rrdPath =  '';
	protected $rrdFile = '';
	protected $device = null;
	protected $startTime = 0;
	protected $endTime = 0;
	protected $start = '-1d';
	protected $height = 200;
	protected $width = 400;
	protected $title = '';
	protected $back = '#FFFFFF00';
	protected $canvas = '#FFFFFF00';
	protected $shadea = '#FFFFFF00';
	protected $shadeb = '#FFFFFF00';
	protected $mgrid = '#999999';
	protected $frame = '#999999';
	protected $font = '#999999';
	protected $grid = '#cccccc';
	protected $axis = '#cccccc';
	protected $arrow = '#FFFFFF00';
	protected $legend = true;
	public function __construct(&$device,$params=array()) {
		$this->device = $device;
		$this->rrdPath = $device->getRrdPath().$this->rrdFile;
		if (is_array($params) || is_object($params)) {
			foreach ($params AS $name=>$value) {
				switch ($name) {
					case 'legend':
						$this->legend = (bool)$value;
						break;
					case 'start':
					case 'title':
					case 'startTime':
					case 'endTime':
						$this->$name = $value;
						break;
					case 'shadea':
					case 'back':
					case 'canvas':
					case 'grid':
					case 'axis':
					case 'mgrid':
					case 'font':
					case 'frame':
					case 'arrow':
						$this->$name = '#'.$value;
						break;
					case 'width':
					case 'height':
						$this->$name = (int)$value;
						break;
					case 'bg':
						$this->back = '#'.$value;
						$this->shadea = '#'.$value;
						$this->shadeb = '#'.$value;
						break;
					case 'border':
						$this->shadea = '#'.$value;
						$this->shadeb = '#'.$value;
						break;
				}
			}
		}
		if (isset($_GET['debug'])) {
			$this->debug = true;
		}
	}
	
	
	public function generate($path) {
		$opts = array_merge($this->getBaseOptions(),$this->getOptions());
		if (!is_array(rrd_graph($path,$opts,count($opts)))) {
			if ($this->debug) {
				echo '<pre>'.print_r($opts,1).'</pre>';
				echo 'Error: '.rrd_error();
				exit();
			}
		}
	}

	protected function getBaseOptions() {
		$opts = array(
			'--start',$this->start,
			'--full-size-mode',
			'--alt-autoscale',
			'--slope-mode',
			'--rigid',
			'-c','BACK'.$this->back,
			'-c','CANVAS'.$this->canvas,
			'-c','SHADEA'.$this->shadea,
			'-c','SHADEB'.$this->shadeb,
			'-c','FRAME'.$this->frame,
			'-c','MGRID'.$this->mgrid,
			'-c','GRID'.$this->grid,
			'-c','ARROW'.$this->arrow,
			'-c','AXIS'.$this->axis,
			'-c','FONT'.$this->font,
			'--font','AXIS:6:mono',
			'--font','UNIT:6:mono',
			'--font','LEGEND:7:mono'
		);
		if (!empty($this->title)) {
			$opts[] = '--title';
			$opts[] = $this->title;
		}

		if (!empty($this->height) && $this->height < 50) { $opts[] = '--only-graph';}
		if (!empty($this->height)) { $opts[] = '--height';$opts[] = $this->height; }
		if (!empty($this->width)) { $opts[] = '--width';$opts[] = $this->width; }
		if (!empty($this->startTime)) { $opts[] = '--start';$opts[] = $this->startTime; }	
		if (!empty($this->endTime)) { $opts[] = '--end';$opts[] = $this->endTime; }	
		if ($this->legend == false) { $opts[] = '--no-legend';}
		return $opts;
	}
	protected function getOptions() {
	
		return array();
	}
}